/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.sys.controller;

import java.util.List;

import com.moon.admin.sys.cache.DictCache;
import com.moon.admin.framework.MoonConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;

import com.moon.admin.sys.entity.SysDictEntity;
import com.moon.admin.sys.domain.query.DictQuery;
import com.moon.admin.sys.service.SysDictService;
import com.moon.admin.framework.util.ToolUtils;
import com.moon.admin.framework.secure.annotation.FormRepeat;
import com.moon.admin.framework.secure.annotation.PreAuth;
import com.moon.admin.framework.springmvc.domain.R;
import com.moon.admin.framework.syslog.annotation.SysLog;
import com.moon.admin.framework.domain.MoonPage;
import com.moon.admin.framework.mybatisplus.query.Condition;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 系统字典 操作处理
 * 
 * @author moon
 */
@RestController
@RequestMapping("/dict/sysDict")
@Api(value="系统字典",tags="系统字典服务")
public class SysDictController{
	
    @Autowired
    private SysDictService sysDictService;

    @ApiOperation(value="系统字典列表")
    @PostMapping(value = "list")
    @PreAuth("dict_sysDict_view")
    public R<MoonPage<SysDictEntity>> list(@RequestBody DictQuery sysDict) {
    	LambdaQueryWrapper<SysDictEntity> lqw = Condition.getQueryWrapper(new SysDictEntity(),sysDict);
    	lqw.eq(SysDictEntity::getParentId, 0);
        IPage<SysDictEntity> pages = sysDictService.page(Condition.getPage(sysDict),lqw);
    	return R.success(MoonPage.of(pages));
    }
    
    @ApiOperation(value="系统字典项列表")
    @GetMapping(value = "childList")
    @PreAuth("dict_sysDict_view")
    public R<List<SysDictEntity>> getChildDictList(Integer parentId) {
    	List<SysDictEntity> list = sysDictService.list(Wrappers.<SysDictEntity>lambdaQuery().eq(SysDictEntity::getParentId, parentId));
    	return R.success(list);
    }

    @ApiOperation(value="系统字典添加/编辑")
    @PostMapping(value = "editSave")
    @PreAuth(value={"dict_sysDict_add","dict_sysDict_edit"})
    @SysLog(mod="系统字典服务",fun="保存")
    @FormRepeat
    public R<String> editSave(@RequestBody SysDictEntity sysDict){
        sysDictService.saveOrUpdate(sysDict);
        return R.success();
    }

    @GetMapping("/removeByIds")
    @ApiOperation(value = "系统字典", notes = "传入ids")
    @PreAuth("dict_sysDict_remove")
    @SysLog(mod="系统字典服务",fun="删除")
    @FormRepeat
    public R<Boolean> removeByIds(@ApiParam(value = "主键集合", required = true)String ids) {
        return R.status(sysDictService.removeByIds(ToolUtils.toIntList(ids)));
    }

    @ApiOperation(value="根据字典编码获取字典项")
    @GetMapping(value = "getTopDictList")
    public R<List<SysDictEntity>> getTopDictList(){
        List<SysDictEntity> list = sysDictService.list(Wrappers.<SysDictEntity>lambdaQuery().eq(SysDictEntity::getParentId, 0).eq(SysDictEntity::getStatus, MoonConstants.DataStatusEnum.enabled.ordinal()));
        return R.success(list);
    }

    @ApiOperation(value="根据字典编码获取字典项")
    @GetMapping(value = "getDictByCode")
    public R<List<SysDictEntity>> getDictByCode(String code){
        List<SysDictEntity> list = DictCache.getDictByCode(code);
    	return R.success(list);
    }

    @ApiOperation(value="修改字典状态")
    @GetMapping("/updateStatusById")
    public R<Void> updateStatusById(Integer id,Integer status){
        SysDictEntity entity = sysDictService.getById(id);
        entity.setStatus(status);
        sysDictService.updateById(entity);
        List<SysDictEntity> list = sysDictService.list(Wrappers.<SysDictEntity>lambdaQuery().eq(SysDictEntity::getCode,entity.getCode()));
        list.forEach(tmp->{
            tmp.setStatus(status);
        });
        sysDictService.updateBatchById(list);
        DictCache.cleanCache();
        return R.success();
    }

    @GetMapping("/cleanCache")
    @ApiIgnore
    public R<Void> cleanCache(){
        DictCache.cleanCache();
        return R.success();
    }
}